﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace RubricaSentimentale.View
{
    public partial class Visualizza : PhoneApplicationPage
    {
        ViewModel vm;
        public Visualizza()
        {
            InitializeComponent();
            
            object st = Utility.LoadState("ViewModel");
            if (st != null)
                vm = (ViewModel)st;
            vm.Current.Initdetails();
            this.DataContext = vm.Current;
            this.information.change += new EventHandler(information_change);
        }

        void information_change(object sender, EventArgs e)
        {
            double height = 0;

            height += (grCont.Children[0] as FrameworkElement).ActualHeight + information.getHeight() + 100;
            if (height > Scroll.ActualHeight)
            {
                grCont.Height = height;
            }
            this.Scroll.InvalidateScrollInfo();
            this.Scroll.UpdateLayout();
        }
        /// <summary>
        /// Override the back button pressed event to reverse the animation of detail view
        /// </summary>
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (information.DetailView)
            {
                e.Cancel = true;
                information.reverse.Begin();
                information.DetailView = false;
            }
            else
            {
                base.OnBackKeyPress(e);
            }
        }


        /// <summary>
        /// Function that remove the curren contact from the list
        /// </summary>
        private void Elimina(object sender, System.EventArgs e)
        {
            var res = MessageBox.Show("Sei Sicuro di volere eliminare il contatto?", "Elimina", MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                vm.agenda.RemovePersona(vm.Current);
                vm.Current = null;
                Utility.Save(vm, typeof(ViewModel));
                NavigationService.GoBack();
            }
        }

        /// <summary>
        /// Invoked when the user click on modify in the application bar
        /// </summary>
        private void btnModifica_Click(object sender, EventArgs e)
        {
            vm.contesto = ViewModel.Context.Edit;
            NavigationService.Navigate(new Uri("/View/AggiungiPersona.xaml", UriKind.Relative));

        }
    }
}